package org.obm.mina;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class MinaCodecFactory implements ProtocolCodecFactory {

	private static final Logger logger = LoggerFactory
			.getLogger(MinaCodecFactory.class);

	protected MinaCodecFactory() {
		logger.info("mina codec factory created");
	}

	public ProtocolEncoder getEncoder(IoSession ios) {
		return new ProtocolEncoderAdapter() {
			public void encode(IoSession session, Object msg,
					ProtocolEncoderOutput out) {
				logger.info("encode: " + msg);
				if (msg instanceof IoBuffer) {
					IoBuffer bb = (IoBuffer) msg;
					out.write(bb);
				}
			}
		};
	}
}
